<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_pingdd/model')->方法名();
*/
namespace jy_pingdd;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	/*
		确认拼单
	*/
	public function checkteam($goodsid,$teamid=0){
		if (!$teamid) {
			//判断团长是否是首次开团
			$hasteam=pdo_get('jy_weishop_groups_team',array('uniacid'=>$_SESSION['uniacid'],'status'=>0,'goodsid'=>$goodsid,'mid'=>$_SESSION['uid']));
			if($hasteam['id']){
				exi('您有一个该商品的拼单还在进行中！','error','jy_pingdd/teamdetail//teamid='.$hasteam['id']);
			}
		}
		$order = pdo_get('jy_weishop_order',array('mid'=>$_SESSION['uid'],'teamid >'=>0,'status'=>0,'uniacid' => $_SESSION['uniacid']));
		if ($order['id']) {
			$checkid = pdo_getcolumn('jy_weishop_order_goods',array('orderid'=>$order['id'],'goodsid' => $goodsid,'uniacid' => $_SESSION['uniacid']),'id');
			if ($checkid) {
				exi('您的该商品拼单已存在，请尽快完成支付！','error','jy_weishop/order.list/');
			}
		}
		if ($teamid) {
			$team=pdo_get('jy_weishop_groups_team',array('id'=>$teamid));
			if ($team['status']>0){
				exi('该拼单已完成，请参与其他拼单！','error','jy_pingdd/index/');
			}
			$goods = pdo_get('jy_weishop_goods',array('id' => $goodsid,'uniacid' => $_SESSION['uniacid']));
			$num = pdo_count('jy_weishop_order',array('mid'=>$_SESSION['uid'],'status'=>10,'teamid'=>$teamid,'uniacid' => $_SESSION['uniacid']));
			if ($num >= $goods['groupnum']) {
				exi('该拼单已完成，请参与其他拼单！','error','jy_pingdd/index/');
			}
		}
		return true;
	}
	/*
		拼团失败
	*/
	public function order_cancel($teamid){
		$params=array('uniacid'=>$_SESSION['uniacid'],'teamid'=>$teamid,'status'=>1);
		$order=pdo_getall('jy_weishop_order',$params,array('id','mid','teamid','price','paytype'));
		foreach($order as $item){
			$check=model('jy_weishop/order/refund')->check_order_rerefund(intval($item['id']));
			if(!$check){
				return false;
			}
			$params=array(
				'orderid'=>intval($item['id']),
				'images'=>$item['images']?ijson_encode($item['images']):'',
				'price'=>$item['price'],
				'content'=>'拼单失败返现',
			);
			model('jy_weishop/order/refund')->refund_create($params,true);
			pdo_update('jy_weishop_order',array('status'=>5),array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['id']));
		}
		pdo_update('jy_weishop_groups_team',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$teamid));
	}
}
?>